package com.retail.member.client.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.retail.core.entity.enums.DelFlagEnum;
import com.retail.core.entity.enums.StatusFlagEnum;
import com.retail.core.entity.result.Result;
import com.retail.member.client.ShopCardClient;
import com.retail.shop.entity.request.ShopCardLogRequest;
import com.retail.shop.entity.request.ShopCardRequest;
import com.retail.shop.entity.vo.RetailShopVo;
import com.retail.shop.entity.vo.ShopCardLogVo;
import com.retail.shop.entity.vo.ShopCardVo;
import com.retail.shop.service.RetailShopService;
import com.retail.shop.service.ShopCardLogService;
import com.retail.shop.service.ShopCardService;
import com.retail.team.enums.VerifyFlagEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ShopCardClientImpl implements ShopCardClient {

    @Autowired
    private ShopCardService shopCardService;

    @Autowired
    private RetailShopService retailShopService;

    @Autowired
    private ShopCardLogService shopCardLogService;


    @Override
    public Result<List<ShopCardVo>> shopAllCards() {
        List<ShopCardVo> list =  shopCardService.shopAllCards();
        return Result.success(list);
    }

    @Override
    public Result<Integer> applyShopCard(ShopCardRequest request) {
        Long adminId = request.getAdminId();
        RetailShopVo shopVo =  retailShopService.getOwnerShopByOwnerId(adminId);
        if (ObjectUtil.isEmpty(shopVo)) {
            return Result.fail("请先完善商户信息！");
        }
        ShopCardLogRequest req = createCardLog(request, shopVo);
        shopCardLogService.save(req);
        return Result.success("权益申请已提交，请稍后！",1);
    }

    public static void main(String[] args) {
        String end = DateUtil.format(DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, 10), "yyyy-MM-dd");
        System.out.println(end);
    }
    private ShopCardLogRequest createCardLog(ShopCardRequest request, RetailShopVo shopVo) {
        ShopCardLogRequest shopCardLogRequest = new ShopCardLogRequest();
        shopCardLogRequest.setShopId(shopVo.getId());
        shopCardLogRequest.setCardType(request.getId());
        shopCardLogRequest.setCardPrice(request.getCardPrice());

        String currentDate = DateUtil.format(new Date(), "yyyy-MM-dd");
        String shopEndDate = shopVo.getCardEndTime();
        if (ObjectUtil.isEmpty(shopEndDate)) {
            shopCardLogRequest.setStartTime(currentDate);
            shopCardLogRequest.setEndTime(DateUtil.format(DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, Integer.parseInt(request.getDateNum()+"")), "yyyy-MM-dd"));
        } else {
            if (DateUtil.parse(currentDate, "yyyy-MM-dd").getTime() < DateUtil.parse(shopEndDate, "yyyy-MM-dd").getTime()) {
                shopCardLogRequest.setStartTime(DateUtil.format(DateUtil.offset(DateUtil.parse(shopEndDate, "yyyy-MM-dd"), DateField.DAY_OF_YEAR, 1), "yyyy-MM-dd"));
                shopCardLogRequest.setEndTime(DateUtil.format(DateUtil.offset(DateUtil.parse(shopCardLogRequest.getStartTime(), "yyyy-MM-dd"), DateField.DAY_OF_YEAR, Integer.parseInt(request.getDateNum()+"")), "yyyy-MM-dd"));
            }
        }

        shopCardLogRequest.setVerifyFlag(VerifyFlagEnums.DAI_SHEN_HE.getValue());
//        shopCardLogRequest.setVerifyTime();
//        shopCardLogRequest.setVerifyRemark();
//        shopCardLogRequest.setId();
        shopCardLogRequest.setDelFlag(DelFlagEnum.NO.getValue());
        shopCardLogRequest.setStatusFlag(StatusFlagEnum.USE.getValue());
//        shopCardLogRequest.setCreateUser();
//        shopCardLogRequest.setCreateTime();
//        shopCardLogRequest.setUpdateTime();
        shopCardLogRequest.setRemark("用户提交审核");

        return shopCardLogRequest;
    }
}
